package org.easydarwin.blogdemos;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

import java.util.ArrayList;
import java.util.List;


public class WaveOutWrapper {
	private boolean isOpen;
	private AudioTrack tracker;
	private String TAG=getClass().getSimpleName();


	public void setSampleRate(int sampleRate) {
		this.sampleRate = sampleRate;
	}

	private int sampleRate = 0;
	private int count=0;

	public WaveOutWrapper() {
		isOpen = false;
	}

	public boolean IsOpen() {
		return isOpen;
	}

	public void Start(int samplesPerSecond) {
		Stop();
		try {
			int bufferSize = AudioTrack.getMinBufferSize(samplesPerSecond,
					AudioFormat.CHANNEL_OUT_MONO,
					AudioFormat.ENCODING_PCM_16BIT);

			/**
			 * 缓冲值会影响声音延迟
			 */
			bufferSize = bufferSize*2;//Howard

			/**
			 * @howard
			 * 使用STREAM_SYSTEM,否则输出切换会异常
			 */

			tracker = new AudioTrack(AudioManager.STREAM_SYSTEM ,
					samplesPerSecond, AudioFormat.CHANNEL_OUT_MONO,
					AudioFormat.ENCODING_PCM_16BIT, bufferSize,
					AudioTrack.MODE_STREAM);

			/**
			 * @howard
			 * 设置辅助效果的level
			 */
			tracker.setVolume(1.0f);
			tracker.play();
			//设置音量后不生效再次尝试
			isOpen = true;
			setSampleRate(samplesPerSecond);
		} catch (Exception e) {
			isOpen = false;
			e.printStackTrace();
		}
		// Log.s("Play Buffer Size:" + bufferSize);
	}
	/**
	 * 音频音量同步:规避AudioTrack初始化后无法播放先前设置的AudioManager音量问题
	 *
	 * @param context    上下文对象
	 * @param streamType 音频同步通道，需与AudioTrack 通道保持一致
	 */
	private void syncAudioVolume(Context context, int streamType) {
		try {
			AudioManager audioManager = (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
			int currentVolume = audioManager.getStreamVolume(streamType);
			audioManager.setStreamVolume(streamType, currentVolume, AudioManager.FLAG_PLAY_SOUND);
		} catch (Exception e) {
//			LogUtils.e(TAG, "syn: error!!!!", e);
		}
	}
	public void Stop() {
		if (isOpen && tracker != null) {
			try {
				// tracker.flush();
				tracker.stop();
				tracker.release();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		tracker = null;
		isOpen = false;

		count=0;
	}

	/**
	 * @howard
	 * 当接收数据少时，调用Pause来缓存数据，解决"滋滋"音
	 * 注意：当调用 pause() 后，需再调用flush() 来清除buffer中没被播放的数据
	 */
	public void Pause() {

		if (isOpen && tracker != null) {
			try {
				tracker.pause();
				tracker.flush();
//				Log.s("tracker pause!");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public void gc() {

		if (isOpen && tracker != null) {
			try {
				tracker.flush();
//				System.gc();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}


	public void continuePlay() {

		if (isOpen && tracker != null) {
			try {
				// tracker.flush();
				tracker.play();
//				Log.s("tracker play!");
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public void WriteData(byte[] data, int offset, int len) {
		if (isOpen && tracker != null) {
			try {
				int res = tracker.write(data, offset, len);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
